package com.example.myapplication.Network;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UdpClient  implements Runnable {
    public String message;
    public String ipAddress;
    public int port;

    @Override
    public void run() {
        sendUDPData(message,ipAddress,port);
    }

    public static  void sendUDPData(String message, String ipAddress, int port) {
        DatagramSocket socket = null;
        try {
            socket = new DatagramSocket();
            InetAddress serverAddr = InetAddress.getByName(ipAddress);
            byte[] sendData = message.getBytes();

            DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, serverAddr, port);
            socket.send(sendPacket);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (socket != null) {
                socket.close();
            }
        }
    }
}
